# !/usr/bin/python3
# _*_ coding: utf-8 _*_
# @Time     : 2021/7/22 上午10:15
# @Author   : HHR
# ___MSG___ : "专利授权日协和端口"
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from quote_folder.Db_connect import DB_connect
from selenium.webdriver.common.by import By
from selenium import webdriver
from bs4 import BeautifulSoup
import time, random, re



class patenauthorization():
	def __init__(self):
		self.get_url = "http://cpquery.cnipa.gov.cn/"
		self.bigdata_con = DB_connect().mysql_mykh()  # 开发组数据库连接
		self.bigdata_cur = self.bigdata_con.cursor()
		self.redis_con = DB_connect().patents_reids()  # redis连接

	def login_gov(self):
		""" 登录中国及多国(协和) """
		profile = webdriver.FirefoxOptions()
		profile.set_preference('network.proxy.type', 1)
		browser = webdriver.Firefox(options=profile, executable_path=r'D:\工具\geckodriver.exe')
		browser.get(self.get_url)
		time.sleep(5)
		WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "username"))).clear()
		# browser.find_element_by_id('username').clear()
		browser.find_element_by_id('username').send_keys('CN00675769')
		time.sleep(1)
		browser.find_element_by_id('password').clear()
		browser.find_element_by_id('password').send_keys('Gv89NWWY@')
		time.sleep(random.randint(30, 50))
		current_url = browser.current_url
		return browser, current_url

	def get_pantent_msg(self, soup, sqh, now_time):
		tr_list = soup.select("div[id='gkggid'] tr")
		if len(tr_list) < 2:
			self.redis_con.sadd("信合红谷接口查询", sqh)
		for tr_tag in tr_list[1:]:
			td_list = tr_tag.select("td")
			openNumber = td_list[1].select("span")[0]["title"]		# 公告（公布）号
			announcement_type = td_list[2].select("span")[0]["title"]			# 公告类型
			impowerDay = td_list[4].select("span")[0]["title"]			# 公告（公布）日
			if "授权公告" in announcement_type:
				print(openNumber, " | ", announcement_type, " | ", impowerDay)
				updata_sql = f"""UPDATE finals SET grantDate='{impowerDay}' WHERE application_number='ZL{sqh}'"""
				try:
					self.bigdata_cur.execute(updata_sql)
				except Exception as E:
					print(E, updata_sql)
		self.bigdata_con.commit()

	def get_msg(self, sqh, browser, current_url, now_time):
		sqh = re.sub(r'ZL', '', sqh)
		pat = ''.join(re.findall(r'select-key:shenqingh=(.*?)&select-key:zhuanlilx', current_url))
		url = re.sub(pat, sqh, current_url)
		browser.get(url)
		count_page = browser.page_source
		soup = BeautifulSoup(count_page, "lxml")
		self.get_pantent_msg(soup, sqh, now_time)

	def main_fun(self):
		now_time = time.strftime("%m%d", time.localtime())
		browser, current_url = self.login_gov()
		need_time = 0
		while self.redis_con.scard("协和端口查询"):
			start_time = time.time()
			application_number = self.redis_con.spop("协和端口查询")
			print('-' * 20, f"开始抓取 {application_number} 的信息, 剩余 {self.redis_con.scard('协和端口查询')} 个, 大约 {need_time} 分钟", '-' * 20)
			self.get_msg(application_number, browser, current_url, now_time)			# 获取专利缴费信息
			self.bigdata_con.commit()
			print()
			print()
			end_time = time.time()
			need_time = round((end_time - start_time) * DB_connect().patents_reids().scard("协和端口查询")/60, 1)
		self.bigdata_cur.close()
		self.bigdata_con.close()
		browser.close()



if __name__ == '__main__':
	patenauthorization().main_fun()
